package com.yang.config.realm;
import com.yang.bean.*;
import com.yang.mapper.MarketAdminMapper;
import com.yang.mapper.MarketPermissionMapper;
import com.yang.mapper.MarketUserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.Mergeable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.8 14:50
 * @description:  可以直接的增加component注册组件
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String type = ((MarketToken) authenticationToken).getType();
        //TODO suject.login传入的AuthenticationToken中的username
        String username = (String) authenticationToken.getPrincipal();
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            MarketAdminExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
            if (marketAdmins.size() == 1) {
                //数据库有唯一与之对应的信息
                //用户信息需要和认证信息建立关系
                MarketAdmin marketAdmin = marketAdmins.get(0);
                //构造认证信息时候，可以放入你需要的信息，可以作为Principals 认证完成以后
                //放入是为了取出,是真实数据库的密码，和AuthenticationToken中的password进行比较
                //第二个参数credenttials
                return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
            }
        } else if ("wx".equals(type)) {
            //查询微信的数据 注意事项user表中的数据
            MarketUserExample example = new MarketUserExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
            if (marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
        }

        return null;
    }

    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //根据用户信息查询权限
        MarketAdmin marketAdmin = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        String roleIds = marketAdmin.getRoleIds();
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(Integer.parseInt(roleIds));
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);

        List<String> list = Arrays.asList("aaa");


        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(list);
        return simpleAuthorizationInfo;


    }
}
